package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAddEditQuestionButtonClick;
import com.example.satu.quizcreator.quiz_creator.interfaces.onFileLoad;
import com.example.satu.quizcreator.quiz_creator.interfaces.onFileRemoveClickListener;
import com.example.satu.quizcreator.quiz_creator.interfaces.onItemFocusChange;
import com.github.rtyley.android.sherlock.roboguice.fragment.RoboSherlockFragment;

import roboguice.inject.InjectView;

import static android.view.View.GONE;
import static android.view.View.OnClickListener;
import static android.view.View.OnFocusChangeListener;
import static android.view.View.VISIBLE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.DATA_TYPE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE.NON_FILE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE.values;

/**
 * Created by Satu on 2014-09-08.
 */
public class CreateQuestionFragment extends RoboSherlockFragment implements onFileLoad,onFileRemoveClickListener {

    public static final String QUESTION_BODY = "body";
    public static final String FILE_TYPE = "FILE_TYPE";
  //  public static final String IMAGE = "IMAGE";
  //  public static final String AUDIO_VIDEO = "AUDIO_VIDEO";
    public static final String FILE_PATH = "file_path";
    @InjectView(R.id.button_add_question)
    ImageButton addQuestionButton;

   // Button editQuestionButton;

    @InjectView(R.id.editText_question_body)
    EditText questionBody;

    @InjectView(R.id.object_loaded_view)
    ImageView loadObjectImageView;

    onAddEditQuestionButtonClick onAddEditQuestionButtonClick;
    onItemFocusChange onItemFocusChange;

    FILE_TYPE file_type = NON_FILE;
/*    Bitmap bitmap;
    byte [] video_sound;*/
    String filePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.question_creator_layout,null);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

            addQuestionButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onAddEditQuestionButtonClick.onAddEditQuestionButton(false);
                }
            });
            questionBody.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    onItemFocusChange.onItemFocus(true,CreateQuestionFragment.this,file_type);
                else
                    onItemFocusChange.onItemFocus(false,null,null);
                }
             });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if( !(getParentFragment() instanceof onAddEditQuestionButtonClick) ){
            throw new RuntimeException("Twoja activity nie implementuje onAddEditQuestionButtonClick");
        }
        onAddEditQuestionButtonClick = (onAddEditQuestionButtonClick) getParentFragment();
        if( !(getActivity() instanceof onItemFocusChange) ){
            throw new RuntimeException("Twoja activity nie implementuje onItemFocusChange");
        }
        onItemFocusChange = (onItemFocusChange) getActivity();
    }

/*    public String getQuestionBody() {
        return questionBody.getText().toString();
    }*/
    public Bundle getQuestionBody() {
        Bundle bundle = new Bundle();
        bundle.putString(QUESTION_BODY,questionBody.getText().toString());
        bundle.putInt(FILE_TYPE,file_type.ordinal());
        bundle.putString(FILE_PATH,filePath);
/*        if(file_type == IMAGE_TYPE)
            bundle.putParcelable(IMAGE,bitmap);
        else if(file_type == AUDIO_TYPE ||file_type == VIDEO_TYPE)
            bundle.putByteArray(AUDIO_VIDEO,video_sound);*/
        return bundle;
    }


    @Override
    public void onObjectLoad(Bundle bundle) {
        file_type = values()[bundle.getInt(DATA_TYPE)];
        clearVariables();
        filePath = bundle.getString(QuizCreatorActivity.FILE_PATH);
        switch (file_type){
            case IMAGE_TYPE:
                //bitmap = bundle.getParcelable(QuizCreatorActivity.IMAGE_TYPE);
                loadObjectImageView.setBackgroundResource(R.drawable.photos);
                loadObjectImageView.setVisibility(VISIBLE);
                break;
            case AUDIO_TYPE:
                //video_sound = bundle.getByteArray(QuizCreatorActivity.AUDIO_TYPE);
                loadObjectImageView.setBackgroundResource(R.drawable.kbemusedsrv);
                loadObjectImageView.setVisibility(VISIBLE);
                break;
            case VIDEO_TYPE:
               // video_sound = bundle.getByteArray(QuizCreatorActivity.VIDEO_TYPE);
                loadObjectImageView.setBackgroundResource(R.drawable.video);
                loadObjectImageView.setVisibility(VISIBLE);
                break;
            default:
                    break;
        }

    }

    @Override
    public void onFileRemoveClick() {
/*        if(file_type != IMAGE_TYPE)
            video_sound = null;
        else
            bitmap = null;*/
        filePath = "";
        loadObjectImageView.setVisibility(GONE);
        file_type = NON_FILE;
    }
    private void clearVariables(){
        filePath = "";
/*        bitmap = null;
        video_sound = null;*/
    }
}
